/*
 *
 */
package org.opu.translator;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  <b>USE:</b> FIRST or LAST
 * @author yura
 */
public class SimpleTokenizer {

    /** get form token list first lexem */
    public static final int FIRST = 0;

    /** get form token list last lexem */
    public static final int LAST = 1;

    /**
     * return true if find in text refular expresion - exp
     * @param text The expression to be compiled
     * @param exp The character sequence to be matche
     * @return  true if find in text refular expresion - exp
     */
    public static boolean isIn(String text, String exp) {
        return text.matches(exp);
    }

    /**
     *  parses the string and finds her tokens to set rules
     * @param source parsesed string
     * @param rules rules on who are looking for tokens
     * @param vget : first or last element
     * @return detected tokens
     */
    public static Token expresion(String text, String exp, int vget) {
        List<Token> t = new RETokenizer()
                .tokenize(text, Arrays.asList(new Rule("NAME", exp)));
        
        if (t == null || t.isEmpty()) {
            return null;
        } else {
            return t.get((t.size() - 1) * vget);
        }
    }

    /**
     *  parses the string and finds her tokens to set rules.
     * vget value is first
     * @param source parsesed string
     * @param rules rules on who are looking for tokens
     * @return detected tokens
     */
    public static Token expresion(String text, String exp) {
        return expresion(text, exp, FIRST);
    }
}
